﻿using System;
using System.Diagnostics;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using SPWarmup;

namespace SPWarmUp.Library
{
    public class FeatureBootStrapper
    {

        /// <summary>
        /// Registers the application.
        /// Registers type mappings for SharePoint Service Locator
        /// Installs the timer job
        /// </summary>
        public void Register()
        {
            RegisterTypeMappings();
            
        }

        /// <summary>
        /// Registers the type mappings.
        /// </summary>
        public void RegisterTypeMappings()
        {
            try
            {
                var config = WarmUpServiceLocator.GetType<IServiceLocatorConfig>();
                WarmUpServiceLocator.RegisterType<ITopologyService,TopologyService>();
                WarmUpServiceLocator.RegisterType<Web.ISpWarmUpConfigController, Web.SPWarmUpConfigController>();

            }
            catch (Exception ex)
            {
                var msg = string.Format("Error in method 'Register Type Mappings. {0},{1}", ex.Message, ex.StackTrace);
                Trace.TraceError(msg);
                throw new Exception(msg);
            }
        }

        public void UnregisterTypeMappings()
        {
            try
            {
                var config = WarmUpServiceLocator.GetType<IServiceLocatorConfig>();
                config.RemoveTypeMappings<ITopologyService>();
                config.RemoveTypeMappings<Web.ISpWarmUpConfigController>();
            }
            catch (System.Exception ex)
            {
                var msg = string.Format("Error in method 'Un-Register Type Mappings. {0},{1}", ex.Message, ex.StackTrace);
                Trace.TraceError(msg);
                throw new Exception(msg);
            }
        }


        /// <summary>
        /// Installs the timer job.
        /// </summary>
        public void InstallTimerJob(SPSchedule schedule)
        {
            try
            {
                //TODO : Safe config (list of webapps) to HOS
                var warmUpJob = new WarmUpTimerJob { Title = Configuration.TimerJobTitle, Name = Configuration.TimerJobName };
                warmUpJob.Schedule = schedule;
                SPFarm.Local.TimerService.JobDefinitions.Add(warmUpJob);
                SPFarm.Local.TimerService.Update(true);
            }
            catch (Exception ex)
            {
                var msg = string.Format("Error in method 'Install timer job. {0},{1}", ex.Message, ex.StackTrace);
                Trace.TraceError(msg);
                throw new Exception(msg);
            }
        }

        /// <summary>
        /// Installs the timer job.
        /// </summary>
        public void UnInstallTimerJob()
        {
            try
            {
                if (SPFarm.Local.TimerService.JobDefinitions.Any(job => job.Title == Configuration.TimerJobTitle))
                {
                    SPFarm.Local.TimerService.JobDefinitions.Where(job => job.Title == Configuration.TimerJobTitle).
                        First().Delete();
                    SPFarm.Local.TimerService.Update(true);
                }


            }
            catch (Exception ex)
            {
                var msg = string.Format("Error in method 'UnInstall timer job. {0},{1}", ex.Message, ex.StackTrace);
                Trace.TraceError(msg);
                throw new Exception(msg);
            }
        }

        /// <summary>
        /// Unregisters the application.
        /// Unregisters type mappings in SharePoint Service Locator
        /// Un-Installs the timer job
        /// </summary>
        public void UnregisterApplication()
        {
            UnInstallTimerJob();
            
        }
    }
}
